﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Nuwanda.TfsSpotlight.Data;
using Nuwanda.Common;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;
using Nuwanda.TfsSpotlight.Views.Servers;
using StructureMap;

namespace Nuwanda.TfsSpotlight.Views.Project
{
    /// <summary>Constructs a tree representation of a list of projects</summary>
    [Author("Bartłomiej Legiędź")]
    [Pluggable("ProjectTreeBuilder")]
    public class ProjectTreeBuilder : IProjectTreeBuilder
    {
        private IMenuService MenuService { get; set; }

        public ProjectTreeBuilder(IMenuService menuService)
        {
            MenuService = menuService;
        }

        public TreeNode[] Build(ProjectDescriptor project)
        {
            var tree = new List<TreeNode>();

            var queryContextMenu = this.MenuService.GetContextMenu(ContextMenusNames.Query);
            var buildContextMenu = this.MenuService.GetContextMenu(ContextMenusNames.Build);

            var privateQueries = CreateQueryGroupNode(Captions.NodePrivate);
            tree.Add(privateQueries);

            var publicQueries = CreateQueryGroupNode(Captions.NodePublic);
            tree.Add(publicQueries);

            foreach (var query in project.Queries)
            {
                var queryNode = CreateQueryNode(query);
                queryNode.ContextMenuStrip = queryContextMenu;

                if (query.IsPrivate)
                    privateQueries.Nodes.Add(queryNode);
                else
                    publicQueries.Nodes.Add(queryNode);
            }

            if (project.BuildTypes.Count > 0)
            {
                var builds = CreateBuildsGroupNode();
                tree.Add(builds);

                foreach (var build in project.BuildTypes)
                {
                    var buildNode = CreateBuildNode(build);
                    buildNode.ContextMenuStrip = buildContextMenu;
                    builds.Nodes.Add(buildNode);
                }
            }

            return tree.ToArray();
        }

        private TreeNode CreateBuildNode(BuildDescriptor buildType)
        {
            return new TreeNode(buildType.BuildType)
            {
                ImageIndex = ((int)ServersNodeIcon.BuildType),
                SelectedImageIndex = ((int)ServersNodeIcon.BuildType),
                Tag = buildType
            };
        }

        private static TreeNode CreateQueryNode(QueryDescriptor query)
        {
            return new TreeNode(query.Name)
                       {
                           ImageIndex = ((int) ServersNodeIcon.Query),
                           SelectedImageIndex = ((int) ServersNodeIcon.Query),
                           Tag = query,
                       };
        }

        private static TreeNode CreateQueryGroupNode(string caption)
        {
            return new TreeNode(caption)
                       {
                           ImageIndex = ((int) ServersNodeIcon.Query),
                           SelectedImageIndex = ((int) ServersNodeIcon.Query)
                       };
        }

        private static TreeNode CreateBuildsGroupNode()
        {
            return new TreeNode(Captions.NodeBuilds)
            {
                ImageIndex = ((int)ServersNodeIcon.Builds),
                SelectedImageIndex = ((int)ServersNodeIcon.Builds)
            };
        }
    }
}